import React, { PropTypes } from 'react';
import constants from '../../../../common/constants';
import { hashHistory} from 'react-router';
import request from '../../../../utils/request';
import {List,Button,WhiteSpace,WingBlank,Toast,Modal} from 'antd-mobile';
import '../less/index.less';

const Item = List.Item;
const alert = Modal.alert;
const mainIconG = constants.mainIconG;
const propTypes = {
    userMsg: PropTypes.string.isRequired
};
class MenuItemList extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            distributorStatus:'',
            supplierStatus:'',
            imageUrl:''

        };
    }

    componentWillReceiveProps(nextProps){
        // this.props.userMsg = nextProps;
        if (this.props.userMsg.user_name !== '') {
            request('UserApplyService', 'queryTaskByUserId', {
                method: 'POST',
                body: {
                    userId: nextProps.userMsg.user_id
                }
            }).then((value) => {
                if (value.data.result === 0) {
                    const {taskList} = value.data.resultData;
                    if (taskList.length === 0) {

                    } else {
                        taskList.forEach((i) => {
                            if (i.task_type === 0) {//分销商申请
                                if (i.status === 0) {
                                    this.setState({
                                        distributorStatus: '待审核'
                                    })
                                } else if (i.status === 1) {
                                    this.setState({
                                        distributorStatus: '审核通过'
                                    })
                                } else if (i.status === 2) {
                                    this.setState({
                                        distributorStatus: '审核不通过'
                                    })
                                }
                            } else if (i.task_type === 1) {//供应商申请
                                if (i.status === 0) {
                                    this.setState({
                                        supplierStatus: '待审核'
                                    })
                                } else if (i.status === 1) {
                                    this.setState({
                                        supplierStatus: '审核通过'
                                    })
                                } else if (i.status === 2) {
                                    this.setState({
                                        supplierStatus: '审核不通过'
                                    })
                                }
                            }

                        });
                    }
                } else if (value.data.result === 1) {
                    Toast.info("网络异常，请稍后再试");
                }
            })
        }
    }

    //退出登录
    loginOut = (e) => {
        const alertInstance = alert('退出登录', '确定退出登录？', [
            { text: '取消', onPress: () => {
                return;
            } ,style: 'default'
            },
            { text: '确定', onPress: () => {
                localStorage.setItem("isLogin", 0);
                this.props.userMsg.user_name = '';
                this.props.userMsg.user_picture = '';
                hashHistory.push({
                    pathname: '/',
                    state: this.props.userMsg
                })
            }
            },
        ]);
        setTimeout(() => {
            // 可以调用close方法以在外部close
            alertInstance.close();
        }, 500000);

        // window.location.href = "./#/";
    }
    //账户安全
    security = (e) => {
        hashHistory.push({
            pathname: '/user/security',
            state: this.props.userMsg
        })
    }
    //分销商申请
    distributorApply = (e) => {
        if (this.props.userMsg.user_name === ''){
            Toast.info("请先登录！",1);
        }else {
            const alertInstance = alert('分销商申请', '确定要申请分销商', [
                { text: '取消', onPress: () => {
                    return;
                } ,style: 'default'
                },
                { text: '确定', onPress: () => {
                    //将申请任务记录到会员申请成为分销商与供应商请求表中
                    request('UserApplyService','addTask', {
                        method: 'POST',
                        body: {
                            userId: this.props.userMsg.user_id,
                            taskType: 0
                        }
                    }).then(function (value) {
                        if (value.data.result === 0){
                            Toast.info("申请成功，待后台审核！",1);
                        }else if (value.data.result === 1){
                            Toast.info("申请失败，请稍后再试！",1);
                        }
                    });
                }
                },
            ]);
            setTimeout(() => {
                // 可以调用close方法以在外部close
                alertInstance.close();
            }, 500000);
        }
    }
    //供应商申请
    supplierApply = (e) => {
        if (this.props.userMsg.user_name === ''){
            Toast.info("请先登录！");
        }else {
            hashHistory.push({
                pathname: '/user/supplierApply',
                state: this.props.userMsg
            })
        }
    }


    render() {
        return (
            <div >
                <List>
                    <Item thumb={mainIconG} multipleLine arrow="horizontal" onClick={this.security}>
                        账户安全
                    </Item>
                    <Item thumb={mainIconG} multipleLine arrow="horizontal" onClick={this.distributorApply}>
                        分销商申请<span style={{color:'grey',fontSize:26}}>&nbsp;&nbsp;&nbsp;{this.state.distributorStatus}</span>
                    </Item>
                    <Item thumb={mainIconG} multipleLine arrow="horizontal" onClick={this.supplierApply}>
                        供应商申请<span style={{color:'grey',fontSize:26}}>&nbsp;&nbsp;&nbsp;{this.state.supplierStatus}</span>
                    </Item>
                </List>
                <div style={{marginTop:'100px'}}>
                    <WingBlank>
                        <Button type="warning" size="large" onClick={this.loginOut}>退出登录</Button><WhiteSpace/>
                    </WingBlank>
                </div>
            </div>
        );
    }
}

MenuItemList.propTypes = propTypes;
export default MenuItemList;